package com.csesteel.crm.dao.impl;

import com.csesteel.common.dao.impl.BaseDaoHibernate4;
import com.csesteel.crm.dao.CrmPriviledgeDao;
import com.csesteel.crm.domain.CrmPriviledge;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by Administrator on 2015/5/20 0020.
 */
@Resource
public class CrmPrivilegeImpl extends BaseDaoHibernate4<CrmPriviledge>
        implements CrmPriviledgeDao {

    public List<CrmPriviledge> findAllCrmPriviledgeByPage(int pageNo, int pageSize, String name) {
        String hql = null;
        if ((name == null) || ("".equals(name))) {
            hql = "select p from CrmPriviledge p order by priviledgeId desc";
        } else {
            hql = "select p from CrmPriviledge p where p.priviledgeCode like '%" + name + "%'order by priviledgeId desc";
        }

        return findByPage(hql, pageNo, pageSize);
    }

    public long findCountUsersWithParams(String name) {
        if ((name != null) && (!"".equals(name))) {
            List<?> l = find("select count(*) from CrmPriviledge p where p.priviledgeCode like '%" + name + "%'order by priviledgeId desc");
            if (l != null && l.size() == 1) {
                return (Long) l.get(0);
            }
        } else {
            return findCount(CrmPriviledge.class);
        }

        // 返回查询得到的实体总数

        return 0;
    }

    public void savePriviledgeByMenu(Integer priviledgeId, Integer menuId) {
        String sql ="insert into CRM_R_MENU_PRIVILEDGE (PK_R_MENU_PRI,priviledge_id,menu_id) values (SEQ_CRM_R_MENU_PRIVILEDGE.Nextval,"+priviledgeId+","+menuId+")";
        saveWithSql(sql);
    }

    public void deletePriviledgeByMenu(Integer priviledgeId) {
        String sql ="delete from crm_r_menu_priviledge p where p.priviledge_id ="+priviledgeId;
        deleteBySQL(sql);
    }
}
